﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using SmartSpace.Silverlight;
using System.Windows.Media;
using System.Windows.Threading;

namespace SmartSpace.Silverlight.Shell.DefaultTheme
{
    public partial class HeaderPanel : SlView, IHeaderPanel
    {
        #region Variables

        private SlUser m_user;

        private SlApplication m_currentApplication;

        public event NavigatingEventHandler Navigating;

        #endregion

        #region Constructors

        public HeaderPanel()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(HeaderPanel_Loaded);
        }

        void HeaderPanel_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
            _ctlTextDate.Text = string.Format("{0}{1}",
                DateTime.Now.ToString("yyyy-MM-dd"),
                System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek));
        }

        void timer_Tick(object sender, EventArgs e)
        {
            _ctlTextTime.Text = DateTime.Now.ToString("HH:mm:ss");
        }

        #endregion

        public void Initiate(IEnumerable<SlShortcut> shortCuts, List<SlApplication> applications, SlApplication currentApplication, SlUser loginUser)
        {
            m_user = loginUser;

            _ctlImgLogo.Source = new BitmapImage(currentApplication.IconUri);
            _ctlAppTitle.Content = currentApplication.Name;
            _ctlTxtUser.Text = loginUser.Name;
            _ctlListShortcuts.ItemsSource = shortCuts;
        }

        public event LogoffEventHandler Logoff;

        public event EventHandler EnterManagement;

        public event ChangeAppEventHandler ChangeApp;

        private void _ctlLinkShortcut_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton button = sender as HyperlinkButton;

            SlShortcut shortcut = button.DataContext as SlShortcut;
            if (Navigating != null)
            {
                Navigating(this, new NavigatingEventArgs(shortcut.Name, shortcut.View));
            }
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (Logoff != null)
            {
                Logoff(this, new LogoffEventArgs(m_user));
            }
        }
    }
}
